package edu.maping.easy.painter;

import javax.media.opengl.GLException;

import processing.core.PApplet;
import codeanticode.glgraphics.GLGraphicsOffScreen;
import codeanticode.glgraphics.GLTexture;
import deadpixel.keystone.CornerPinSurface;
import deadpixel.keystone.Keystone;
import edu.maping.easy.gui.ButtonList;
import edu.maping.easy.gui.GuiFactory;
import edu.maping.easy.model.PolygonThing;
import edu.maping.easy.model.Thing;
import edu.maping.easy.painter.cornerpin.CornerPin;

public class PainterContainer {

	public ThingPainterDelegate painterDelegate;
	CornerPin cornerPin;
	PApplet applet;

	ButtonList buttonList;
	GuiFactory guiFactory;

	public PainterContainer() {
	}

	public void setup(PApplet pApplet, ThingPainterDelegate painterDelegate,
			ButtonList buttonList, GuiFactory guiFactory) {
//
//		this.guiFactory = guiFactory;
//		this.buttonList = buttonList;
//
//		applet = pApplet;
//		this.painterDelegate = painterDelegate;
//		try {
//			offscreen = new GLGraphicsOffScreen(pApplet, 320, 240, true, 4);
//
//		} catch (GLException e) {
//			e.printStackTrace();
//			return;
//		}
//
//		ks = new Keystone(pApplet);
//		surface = ks.createCornerPinSurface(320, 240, 20);
//		
//		if (painterDelegate instanceof CornerPin) {
//			((CornerPin) painterDelegate).setup(pApplet, offscreen, surface,
//					buttonList, guiFactory);
//		} else {
//			painterDelegate.setup(pApplet, null, buttonList, guiFactory);
//		}
//		
//		cornerPin = (CornerPin) painterDelegate;
//		ks.toggleCalibration();

	}

//	public void draw(Thing body, PApplet pApplet) {
//		if (offscreen == null) {
//			painterDelegate.draw(body, pApplet);
//		} else {
//			offscreen.beginDraw();
//			cornerPin.draw(body, pApplet, offscreen, null);
//			offscreen.endDraw();
//			GLTexture glTexture = offscreen.getTexture();
//			// glTexture.
//			surface.render(glTexture);
//		}
//	}

	public PainterContainer createNewInstance() {

		PainterContainer container = new PainterContainer();
		ThingPainterDelegate painterDelegate = this.painterDelegate
				.createNewInstance();

//		if (offscreen != null) {
//			// if (thing instanceof PolygonThing) {
//			// PolygonThing polygonThing = (PolygonThing) thing;
//			container.setup(applet, painterDelegate, buttonList, guiFactory);
//			// if (polygonThing.vertex.size() == 5) {
//			// // Ya lo adaptamos
//			// //container.ks.
//			// }
//			// }
//
//		} else {
			container.painterDelegate = painterDelegate;
//		}

		return container;
	}
}
